package com.op.teachingliteapp.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.fragment.SendNotifFragment;
import com.op.teachingliteapp.fragment.SendPostFragment;
import com.op.teachingliteapp.fragment.notif.NotifFragment;
import com.op.teachingliteapp.fragment.resource.ResouceFragment;
import com.op.teachingliteapp.fragment.seminar.SeminarFragment;
import com.op.teachingliteapp.sender.Constants;
import com.op.teachingliteapp.sender.Runtime;
import com.op.teachingliteapp.sender.runnable.SendCommandRunnable;
import com.op.teachingliteapp.sender.ui.view.device.DeviceScanDialog;
import com.op.teachingliteapp.util.CommandSendUtil;
import com.op.teachingliteapp.util.ScreenSendUtils;
import com.op.teachingliteapp.util.ToastUtils;
import net.loonggg.bar.Course;

/*
 * 这是第二个界面的主Activity
 */
public class CourseActivity extends Activity {

    private Button sendPost;
    private Button sendNotif;// 发送通知

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        if (getFragmentManager().findFragmentById(R.id.main_show) == null) {
            switchFragment(new ResouceFragment());
        }

        // 返回
        findViewById(R.id.back).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        // 教学资料
        findViewById(R.id.button_data).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                findViewById(R.id.button_data).setBackgroundResource(R.drawable.button_data1);
                findViewById(R.id.button_discussion).setBackgroundResource(R.drawable.button_discussion3);
                findViewById(R.id.button_notice).setBackgroundResource(R.drawable.button_notice3);
                sendPost.setVisibility(View.GONE);// 隐藏发布帖子按钮
                switchFragment(new ResouceFragment());
                sendNotif.setVisibility(View.GONE);// 隐藏发布通知按钮

            }
        });

        // 课程讨论
        findViewById(R.id.button_discussion).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                findViewById(R.id.button_data).setBackgroundResource(R.drawable.button_data3);
                findViewById(R.id.button_discussion).setBackgroundResource(R.drawable.button_discussion1);
                findViewById(R.id.button_notice).setBackgroundResource(R.drawable.button_notice3);
                Toast.makeText(getApplicationContext(), "课程讨论", Toast.LENGTH_SHORT).show();
                sendPost.setVisibility(View.VISIBLE);// 显示发布帖子按钮
                sendNotif.setVisibility(View.GONE);// 隐藏发布通知按钮

                switchFragment(new SeminarFragment());

            }
        });

        // 课程通知
        findViewById(R.id.button_notice).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                findViewById(R.id.button_data).setBackgroundResource(R.drawable.button_data3);// 杩樺師
                findViewById(R.id.button_discussion).setBackgroundResource(R.drawable.button_discussion3);// 杩樺師
                findViewById(R.id.button_notice).setBackgroundResource(R.drawable.button_notice1);// 鎸変笅
                Toast.makeText(getApplicationContext(), "课程通知", Toast.LENGTH_SHORT).show();
                sendPost.setVisibility(View.GONE);// 隐藏发布帖子按钮
                sendNotif.setVisibility(View.VISIBLE);// 显示发布通知按钮

                switchFragment(new NotifFragment());

                Log.i("XXXXX", "XXXXXX");
            }
        });

        // 投影
        // -------------------------------------------------------------------------------
        ImageButton sendShow = (ImageButton) findViewById(R.id.send_show);
        Button deviceScanBtn = (Button) findViewById(R.id.device_scan);
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.openOrclose);
        Runtime.refreshStatus(sendShow, deviceScanBtn, radioGroup);
        sendShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Runtime.isOffLine()) {

                    new DeviceScanDialog(CourseActivity.this).show();

                } else {
                    if (Runtime.isSend()) {
                        Runtime.pause();
                        Runtime.refreshStatus();

                    } else {

                        Runtime.play();
                        Runtime.refreshStatus();
                        ScreenSendUtils.send();
                    }

                }
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (Runtime.getIp() != null) {
                    switch (checkedId) {
                        case R.id.openStudent:
                            CommandSendUtil.openStudentPort(CourseActivity.this);
                            break;
                        case R.id.closeStudent:
                            CommandSendUtil.closeStudentPort(CourseActivity.this);
                            break;
                    }
                }
            }

        });

        deviceScanBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new DeviceScanDialog(CourseActivity.this).show();

            }
        });

        // 更多
        findViewById(R.id.more).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ToastUtils.show(getApplicationContext(), "更多");
            }
        });

        // 发布帖子
        sendPost = (Button) findViewById(R.id.post);
        sendPost.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, new SendPostFragment()).commit();
            }
        });

        sendNotif = (Button) findViewById(R.id.post_message);
        sendNotif.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                getFragmentManager().beginTransaction().replace(R.id.main_show2, new SendNotifFragment()).addToBackStack(null).commit();

            }
        });

    }

    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.i("CourseActivity", "onPostResume");
    }

    /*
     * 清空堆栈再添加fragment
     */
    private void switchFragment(Fragment fragment) {
        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);// 清空堆栈
        getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, fragment).commit();

    }

    @Override
    public void onBackPressed() {
        /*
		 * 判断堆栈是否只有一个
		 */
        if (getFragmentManager().getBackStackEntryCount() == 1) {
            finish();
            return;
        }
		/*
		 * 不是一个的调用系统的onBackPressed
		 */
        super.onBackPressed();
    }
}
